//
//  ViewController.m
//  简易相册
//
//  Created by 苗钧豪 on 16/7/20.
//  Copyright © 2016年 苗钧豪. All rights reserved.
//

#import "ViewController.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define kheight [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UIScrollViewDelegate>
@property(nonatomic,retain)NSTimer *timer;
@property(nonatomic)NSInteger number;
@property(nonatomic,strong)UIScrollView *scrollView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _scrollView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    _scrollView.contentSize = CGSizeMake(5 * WIDTH, kheight);
    _scrollView.backgroundColor = [UIColor yellowColor];
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.minimumZoomScale = 0.5;
    _scrollView.maximumZoomScale = 2.0;
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
    for (int i = 0; i < 4; i ++) {
        UIScrollView *scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake((i + 1) * WIDTH, 0, WIDTH, kheight)];
        scrollview.contentSize = CGSizeMake(WIDTH, kheight);
        scrollview.backgroundColor = [UIColor greenColor];
        scrollview.maximumZoomScale = 2.0;
        scrollview.minimumZoomScale = 0.5;
        scrollview.delegate = self;
        UIImageView *imgview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, kheight)];
        imgview.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i + 1]];
        [scrollview addSubview:imgview];
        [self.scrollView addSubview:scrollview];
    }
    self.number = 1;
    UIScrollView *scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, kheight)];
    scrollview.contentSize = CGSizeMake(WIDTH, kheight);
    scrollview.backgroundColor = [UIColor greenColor];
    UIImageView *imgview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, kheight)];
    imgview.image = [UIImage imageNamed:@"4.jpg"];
    [scrollview addSubview:imgview];
    [self.scrollView addSubview:scrollview];
    NSTimer *timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(Action:) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    _timer = timer;
}
- (void)Action:(UIScrollView *)scrollview
{
    if (self.number < 4) {
        if (self.number == 1) {
            self.scrollView.contentOffset = CGPointMake(0, 0);
        }
        [self.scrollView setContentOffset:CGPointMake(self.number * WIDTH, 0) animated:YES];
    }else{
        [self.scrollView setContentOffset:CGPointMake(self.number * WIDTH, 0) animated:YES];
        self.number = 0;
    }
    self.number ++;
}
// 指定缩放界面
- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return [scrollView.subviews firstObject];
}
// 保证缩放过程中 照片位于界面中心
- (void)scrollViewDidZoom:(UIScrollView *)scrollView NS_AVAILABLE_IOS(3_2)
{
    //先获取imageview
    UIImageView *imview = [scrollView.subviews objectAtIndex:0];
    if (scrollView.zoomScale <= 1.0)
    {
        imview.center = CGPointMake(WIDTH / 2, kheight / 2);
    }
}
// 保证翻页后照片大小还原
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    for (UIScrollView *scrollview in scrollView.subviews) {
        if ([scrollview isKindOfClass:[UIScrollView class]]) {
            scrollview.zoomScale = 1.0;
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
